package org.itzixi.netty.utils;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.itzixi.pojo.netty.NettyServerNode;
import org.itzixi.utils.JsonUtils;

import java.net.InetAddress;
import java.util.List;

public class ZookeeperRegister {
    public static void registerNettyServer(String nodeName,
                                           String ip,
                                           Integer port) throws Exception {
        CuratorFramework zkClient = CuratorConfig.getClient();
        String path = "/" + nodeName;
        Stat stat = zkClient.checkExists().forPath(path);
        if (stat == null) {
            zkClient.create()
                    .creatingParentsIfNeeded()
                    .withMode(CreateMode.PERSISTENT)
                    .forPath(path);
        } else {
            System.out.println(stat);
        }

        // 创建对应的临时节点，值可以放在线人数，默认为初始化的0
        NettyServerNode nettyServerNode = new NettyServerNode();
        nettyServerNode.setIp(ip);
        nettyServerNode.setPort(port);
        String nodeJson = JsonUtils.objectToJson(nettyServerNode);
        zkClient.create()
                .withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
                .forPath(path + "/im-", nodeJson.getBytes());
    }

    public static String getLocalIp() throws Exception {
        InetAddress address = InetAddress.getLocalHost();
        String ip = address.getHostAddress();
        System.out.println("本机IP地址：" + ip);
        return ip;
    }

    public static void incrementOnlineCounts(NettyServerNode serverNode) throws Exception {
        // TODO 在线人数的增减操作
        dealOnlineCounts(serverNode, 1);

    }

    public static void decrementOnlineCounts(NettyServerNode serverNode) throws Exception {
        // TODO 在线人数的增减操作
        dealOnlineCounts(serverNode, -1);

    }

    public static void dealOnlineCounts(NettyServerNode serverNode, Integer counts) throws Exception {
        // TODO 在线人数的增减操作
        CuratorFramework zkClient = CuratorConfig.getClient();

        InterProcessReadWriteLock readWriteLock =
                new InterProcessReadWriteLock(zkClient, "/rw-lock");
        readWriteLock.writeLock().acquire();

        try {
            String path = "/server-list";
            List<String> list = zkClient.getChildren().forPath(path);

            for (String node : list) {
                String nodePath = path + "/" + node;
                String nodeValue = new String(zkClient.getData().forPath(nodePath));
                NettyServerNode pendingNode = JsonUtils.jsonToPojo(nodeValue, NettyServerNode.class);
                if(pendingNode.getIp().equals(serverNode.getIp())&&
                   pendingNode.getPort().intValue() == serverNode.getPort().intValue()){
                    pendingNode.setOnlineCounts(pendingNode.getOnlineCounts() + counts);
                    String nodeJson = JsonUtils.objectToJson(pendingNode);
                    zkClient.setData().forPath(nodePath, nodeJson.getBytes());
                }
            }
        } finally {
            readWriteLock.writeLock().release();
        }



    }
}
